export const isObject = (val) => val !== null && typeof val === "object";

export const isFunction = (val) => typeof val === "function";

/**
 * 随机生成数字
 *
 * 示例：生成长度为 12 的随机数：randomNumber(12)
 * 示例：生成 3~23 之间的随机数：randomNumber(3, 23)
 *
 * @param1 最小值 | 长度
 * @param2 最大值
 * @return int 生成后的数字
 */
export function randomNumber() {
  // 生成 最小值 到 最大值 区间的随机数
  const random = (min, max) => {
    return Math.floor(Math.random() * (max - min + 1) + min);
  };
  if (arguments.length === 1) {
    // eslint-ignore-=ne

    // eslint-disable-next-line
    const [length] = arguments;
    // 生成指定长度的随机数字，首位一定不是 0
    const nums = [...Array(length).keys()].map((i) =>
      i > 0 ? random(0, 9) : random(1, 9)
    );
    return parseInt(nums.join(""));
  } else if (arguments.length >= 2) {
    // eslint-disable-next-line
    const [min, max] = arguments;
    return random(min, max);
  } else {
    return Number.NaN;
  }
}
/**
 * 随机生成字符串
 * @param length 字符串的长度
 * @param chats 可选字符串区间（只会生成传入的字符串中的字符）
 * @return string 生成的字符串
 */
export function randomString(length, chats) {
  if (!length) length = 1;
  if (!chats) chats = "0123456789qwertyuioplkjhgfdsazxcvbnm";
  let str = "";
  for (let i = 0; i < length; i++) {
    const num = randomNumber(0, chats.length - 1);
    str += chats[num];
  }
  return str;
}
/**
 * 随机生成uuid
 * @return string 生成的uuid
 */
export function randomUUID(len = 5) {
  const chats = "0123456789abcdef";
  return randomString(len, chats);
}

export function downloadCode(code, fileName) {
  const url = window.URL.createObjectURL(new Blob([code]));
  const a = document.createElement("a");
  a.style.display = "none";
  a.href = url;
  a.setAttribute("download", fileName);
  document.body.appendChild(a);
  a.click();
  document.body.removeChild(a);
  window.URL.revokeObjectURL(url);
}

export async function readClipText() {
  const clipText = await navigator.clipboard.readText();
  return clipText;
}

export function isTableHelperColumn(label) {
  return ["序号", "操作"].includes(label);
}

const defaultValueMap = {
  select: undefined,
  "dict-select": undefined,
  "range-picker": [],
  "range-time-picker": [],
  "upload-file": [],
  "preview-media": [],
};
export function getFieldDefaultValue(type) {
  return defaultValueMap[type] || "";
}

export function genFieldMapField(field) {
  const { type, label, formKey, fieldList, genConfig } = field;
  const result = {
    label,
    type: "string",
    beforeKey: formKey,
  };
  if (type === "subForm") {
    result.type = genConfig.subFormType === "multiRow" ? "array" : "object";
    result.children = [];
    for (const subField of fieldList) {
      if (!isTableHelperColumn(subField.label)) {
        result.children.push(genFieldMapField(subField));
      }
    }
  }
  return result;
}

export function cotainsStr(a, b) {
  let flag = a.startsWith(b);
  if (!flag) {
    flag = a.endsWith(b);
  }
  if (!flag) {
    flag = a.includes(b);
  }
  if(!flag){
    flag = b.startsWith(); 
  }
  if (!flag) {
    flag = b.endsWith(a);
  }
  if (!flag) {
    flag = b.includes(a);
  }
  return flag
}